#include "VisualizationWrapper.h"
#include "ValueWithError.h"

#include <iostream>

void VisualizationWrapper::Configure(bool draw,  bool print)
{
    m_draw = draw;
    m_print = print;

    m_pad = 1;

    if( m_draw  )
    {    
        if( m_canvas == NULL  )
        {
            m_canvas = new TCanvas("TomsonSignals", "Signals", -1);
            m_canvas->Divide(2, m_chanelsShow);
            m_canvas->Connect("Closed()", "VisualizationWrapper", (void*)this, "CanvasClosed()");
            m_canvas->SetEditable(false);
        }
        else
        {
            m_canvas->SetEditable(true);
            m_canvas->Clear("D");
            m_canvas->Update();
            m_canvas->SetEditable(false);
        }
    }
    else
        m_canvas = NULL;
};

void VisualizationWrapper::DrawEmpty() 
{
    if( !m_draw  )
        return;

    if(!m_canvas)
    {
        std::cout << "Error: canvas not found\n";
        return;
    }

    m_pad++;
    
    if( m_pad == 2*m_chanelsShow+1 )
    {
        m_canvas->SetEditable(true);
        m_canvas->Update();
        m_canvas->SetEditable(false);
    }
}

void VisualizationWrapper::DrawSignal(std::vector<double> signalData, double maxY, double minY, float signalStart, float signalEnd, int signal, bool filtered)
{
    if( !m_draw  )
        return;

    if(!m_canvas)
    {
        std::cout << "Error: canvas not found\n";
        return;
    }

    m_canvas->SetEditable(true);
    m_canvas->cd(m_pad++);

    char signalName[30];
    
    char *filt;
    if(filtered)
        filt = "filtered";
    else
        filt = "";

    sprintf(signalName, "Chanel %d %s", signal, filt);

    char n[30];
    sprintf(n, "hist_%d_%s\0", signal, filt);

    TH1F *h = new TH1F(signalName, signalName, signalData.size(), signalStart, signalEnd);

    for(int i = 0; i < signalData.size(); ++i )
    {
        h->SetBinContent(i, signalData[i]);
    }

    h->SetDirectory(0);
    h->SetMaximum(maxY);
    h->SetMinimum(minY);
    h->SetBit(kCanDelete);
    h->Draw();

    if( m_pad == 2*m_chanelsShow+1 )
        m_canvas->Update();

    m_canvas->SetEditable(false);
}

void VisualizationWrapper::PrintVector(std::string name, std::vector<ValueWithError> &vals)
{
    if( !m_print  )
        return;

    std::cout << "\n" << name << std::endl;
    for( int i = 0; i < vals.size(); ++i )
    {
        std::cout << "\t" << i << ". " << vals[i].val << " +/-" << vals[i].error << "\t" << std::endl;
    }
}

void VisualizationWrapper::PrintValue(std::string name, ValueWithError &val)
{
    if( !m_print  )
        return;

    std::cout << "\n" << name << std::endl;
    std::cout << "\t" << val.val << " +/-" << val.error << "\t" << std::endl;
}

void VisualizationWrapper::PrintVector(std::string name, std::vector<double> &vals)
{
    if( !m_print  )
        return;

    std::cout << "\n" << name << std::endl;
    for( int i = 0; i < vals.size(); ++i )
    {
        std::cout << "\t" << i << ". " << vals[i] << "\t" << std::endl;
    }
}

void VisualizationWrapper::PrintValue(std::string name, double &val)
{
    if( !m_print  )
        return;

    std::cout << "\n" << name << std::endl;
    std::cout << "\t" << val << "\t" << std::endl;
}
